package com.jd.header.adapter;

import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;

import androidx.annotation.NonNull;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.jd.header.entity.PhotoEntity;
import com.jd.header.R;
import com.jd.header.util.DisplayUtil;

import java.util.HashSet;

public class PhotoAdapter extends BaseQuickAdapter<PhotoEntity.ResBean.VerticalBean, BaseViewHolder> {

    private HashSet<Integer> selectMap = null;

    public PhotoAdapter() {
        super(R.layout.item_photo);
        selectMap = new HashSet<>();
    }

    public HashSet<Integer> getSelectMap() {
        return selectMap;
    }

    @Override
    protected void convert(@NonNull BaseViewHolder baseViewHolder, PhotoEntity.ResBean.VerticalBean userEntity) {
        ImageView imgCover = baseViewHolder.getView(R.id.imgCover);
        ImageView imgCheck = baseViewHolder.getView(R.id.imgCheck);
        View viewSelect = baseViewHolder.getView(R.id.viewSelect);

        if (selectMap.contains(getItemPosition(userEntity))) {
            imgCheck.setImageResource(R.mipmap.icon_item_class_album_details_select_true);
            viewSelect.setVisibility(View.VISIBLE);
        } else {
            imgCheck.setImageResource(R.mipmap.icon_item_class_album_details_select_false);
            viewSelect.setVisibility(View.GONE);
        }

        FrameLayout.LayoutParams layoutParams = (FrameLayout.LayoutParams) imgCover.getLayoutParams();
        layoutParams.width = (DisplayUtil.getScreenWidth(getContext()) - DisplayUtil.dip2px(getContext(), 12f)) / 3;
        layoutParams.height = (DisplayUtil.getScreenWidth(getContext()) - DisplayUtil.dip2px(getContext(), 12f)) / 3;
        imgCover.requestLayout();

        FrameLayout.LayoutParams vlayoutParams = (FrameLayout.LayoutParams) viewSelect.getLayoutParams();
        vlayoutParams.width = (DisplayUtil.getScreenWidth(getContext()) - DisplayUtil.dip2px(getContext(), 12f)) / 3;
        vlayoutParams.height = (DisplayUtil.getScreenWidth(getContext()) - DisplayUtil.dip2px(getContext(), 12f)) / 3;
        viewSelect.requestLayout();

        imgCheck.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (selectMap.contains(getItemPosition(userEntity))) {
                    selectMap.remove(getItemPosition(userEntity));
                } else {
                    selectMap.add(getItemPosition(userEntity));
                }
                notifyItemChanged(getItemPosition(userEntity));
            }
        });


        Glide.with(getContext())
                .load(userEntity.getPreview())
                .skipMemoryCache(false)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .into(imgCover);
    }

    public void selectRange(int start, int end, boolean selected) {
        for (int i = start; i <= end; i++) {
            if (selected)
                selectMap.add(i);
            else
                selectMap.remove(i);
        }
        notifyItemRangeChanged(start, end - start + 1);
    }
}